/***************************************************************************
 *   Copyright (C) 2007 by Phillip Aldridge   *
 *   phillip@imi21.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "iconwind.h"
#include "config.h"
#include <qfont.h>
#include <qcolor.h>
#include <qpen.h>
#include <qdatetime.h>
#include <qpointarray.h>
#include <qpixmap.h>

/**
   This draws all the the wind icon
   TODO display compass points

*/
IconWind::IconWind(QWidget *parent, const char *name)
 : QWidget(parent, "iconWind")
{
   qDebug("Setting up iconWind");
   width=100; height=100;
   setFixedSize (width,height);
   maxWind=0.00;
}


IconWind::~IconWind()
{
}
void IconWind::setStationData(StationData *s)
{
   stationdata=s;
}

void IconWind::save()
{
   const QString file=QString("%1/%2").arg(_FTP_LOCAL_FILES).arg(_FTP_WIND);
   QPixmap pm( width, height );
   bitBlt( &pm,0,0, this, 0,0);
   pm.save(file,"PNG",80);
}

void IconWind::paintEvent( QPaintEvent *e )
{ 
   float angle = stationdata->windAngle();
   float speed = stationdata->windSpeed();
   maxWind=QMAX(maxWind, speed);
   float maxSpeed =  maxWind;

   QPainter painter(this);
   QPen pen = painter.pen();

  //  fill background
   painter.fillRect(0, 0, width, height, Qt::white);

   painter.save();
   painter.setWindow( -50,-50, 100,100 );
   QRect v = painter.viewport();
   int d = QMIN( v.width(), v.height() );
   painter.setViewport( v.left() + (v.width()-d)/2,
                            v.top() + (v.height()-d)/2, d, d );

   //draw compass
   painter.save();
   pen.setColor( Qt::gray );
   pen.setJoinStyle(Qt::BevelJoin);
   painter.setPen( pen );
   painter.drawEllipse (-40,-40,80,80);
   painter.restore();

// draw current max wind speed for the day
  painter.save();
   maxSpeed = -(maxWind * 3)*16;
   painter.setBrush( Qt::darkGreen );
   painter.drawPie (-30,-30,60,60,-179*16,(int)maxSpeed);
   painter.restore();

// draw current wind speed
   painter.save();
  // pen.setStyle ( NoPen );
   speed=-(speed * 3)*16;
   painter.setBrush( Qt::green );
   painter.drawPie (-20,-20,40,40,-179*16,(int)speed);
  painter.restore();

//draw indicator
   painter.save();
   QPointArray pts;
  // painter.setPen( NoPen );
   painter.setBrush( Qt::red );
   painter.rotate(angle-90);
   pts.setPoints( 4, 0,0, 45,-3, 49,0, 45,3 );
   painter.drawConvexPolygon( pts );
  painter.restore();
// draw the text
painter.save();
QFont ft( "Helvetica", 8, QFont::Bold );
    painter.setFont( ft );
    painter.setPen( Qt::black );
    painter.drawText( -45, -40, tr("Wind") );
    painter.drawText( 20, -40, stationdata->windDirection() );
    painter.drawText( -45, 47, QString("%1%2").arg(stationdata->windSpeed()).arg(_UNIT_SPEED) );
    painter.drawText( 8, 47, stationdata->windChill() + _UNIT_TEMP );
 painter.restore();
  painter.restore();
  save();
}

